package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.GetRoomsWithDeviceByFloorIdVO;
import com.zzyl.nursing.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Slf4j
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private StringRedisTemplate redis;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间ID查询房间数据
     *
     * @param id 房间ID
     * @return 查到的结果
     */
    @Override
    public RoomVo getRoomVoByRoomId(Integer id) {
        return roomMapper.getRoomVoByRoomId(id);
    }

    /**
     * 查询指定楼层下的所有有智能设备的房间数据和床位数据
     *
     * @param id 楼层id
     * @return 数据
     */
    @Override
    public List<GetRoomsWithDeviceByFloorIdVO> getRoomsWithDeviceByFloorId(Long id) {

        List<GetRoomsWithDeviceByFloorIdVO> vo = roomMapper. getRoomsWithDeviceByFloorId(id);
        for (GetRoomsWithDeviceByFloorIdVO dd : vo) {
            //房间设备
            List<GetRoomsWithDeviceByFloorIdVO.DeviceVO> deviceVos = dd.getDeviceVos();
            for (GetRoomsWithDeviceByFloorIdVO.DeviceVO deviceVo : deviceVos) {
                try {
                    List<GetRoomsWithDeviceByFloorIdVO.DeviceDataVo> redisByIotId = getRedisByIotId(deviceVo.getIotId());
                    deviceVo.setDeviceDataVos(redisByIotId);
                }catch (Exception e){
                    log.error("设备未启动:{}",e.getMessage());
                }
            }
        }
        for (GetRoomsWithDeviceByFloorIdVO aa : vo) {
            List<GetRoomsWithDeviceByFloorIdVO.BedVo> bedVoList = aa.getBedVoList();
            for (GetRoomsWithDeviceByFloorIdVO.BedVo bedVo : bedVoList) {
                List<GetRoomsWithDeviceByFloorIdVO.DeviceVO> deviceVos1 = bedVo.getDeviceVos();
                if (!ObjectUtil.isEmpty(deviceVos1)){
                    for (GetRoomsWithDeviceByFloorIdVO.DeviceVO deviceVO : deviceVos1) {
                        try {
                            List<GetRoomsWithDeviceByFloorIdVO.DeviceDataVo> redisByIotId = getRedisByIotId(deviceVO.getIotId());
                            deviceVO.setDeviceDataVos(redisByIotId);
                        }catch (Exception e){
                            log.error("设备未启动:{}",e.getMessage());
                        }

                    }
                }
            }
        }

//        vo.stream().map((v)->{
//            v.getDeviceVos().stream().map((deviceData)->{
//                List<GetRoomsWithDeviceByFloorIdVO.DeviceDataVo> roomDeviceDataVos = getRedisByIotId(deviceData.getIotId());
//                deviceData.setDeviceDataVos(roomDeviceDataVos);
//                return deviceData;
//            });
//            return v;
//        }).collect(Collectors.toList());

//        vo.stream().map((v)->{
//            v.getBedVoList().stream().map((bedVo)->{
//                bedVo.getDeviceVos().stream().map((d)->{
//                    List<GetRoomsWithDeviceByFloorIdVO.DeviceDataVo> bedDeviceDataVos = getRedisByIotId(d.getIotId());
//                    d.setDeviceDataVos(bedDeviceDataVos);
//                    return d;
//                }).collect(Collectors.toList());
//                return bedVo;
//            });
//            return v;
//        }).collect(Collectors.toList());
        return vo;
    }
    public List<GetRoomsWithDeviceByFloorIdVO.DeviceDataVo> getRedisByIotId(String iotId)throws Exception{
        Object jsonStr =  redis.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
        List<GetRoomsWithDeviceByFloorIdVO.DeviceDataVo> deviceData = JSONUtil.toList(jsonStr.toString(), GetRoomsWithDeviceByFloorIdVO.DeviceDataVo.class);
        return deviceData;
    }
}
